/**
 * GTBarButton.m
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License 
 * at http://www.mozilla.org/MPL/
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 * either express or implied. See the License for the specific language governing rights and limitations 
 * under the License.
 *
 * The Original Code is gcast-toolbox4iphone.
 *
 * The Initial Developers of the Original Code is Giannandrea Castaldi.
 */
#import "GTBarButtonWithUIBorderButton.h"

static const float kButtonFreeSpace = 14;
static const float kSpaceBetweenIconAndText = 5;

@interface GTBarButtonWithUIBorderButton ()
@property(nonatomic,retain,readwrite) UIButton *button;
- (UIButton*)customButtonWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon;
- (UIButton*)customButtonWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon buttonWidth:(CGFloat)buttonWidth;
- (void)buttonPressed;
@end

@implementation GTBarButtonWithUIBorderButton
@synthesize button;

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon {
  UIButton *newButton = [self customButtonWithTitle:aTitle font:aFont icon:anIcon];
  if ((self = [super initWithCustomView:newButton])) {
    self.button = newButton;
    [self.button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];    
  }
  return self;
}

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon buttonWidth:(CGFloat)buttonWidth {
  UIButton *newButton = [self customButtonWithTitle:aTitle font:aFont icon:anIcon buttonWidth:buttonWidth];
  if ((self = [super initWithCustomView:newButton])) {
    self.button = newButton;
    [self.button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];    
  }
  return self;
}

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont {
  return [self initWithTitle:aTitle font:aFont icon:nil];
}

- (id)initWithTitle:(NSString*)aTitle font:(UIFont*)aFont buttonWidth:(CGFloat)buttonWidth {
  return [self initWithTitle:aTitle font:aFont icon:nil buttonWidth:buttonWidth];
}

- (void)dealloc {
  [button release];
  [super dealloc];
}

- (UIButton*)customButtonWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon buttonWidth:(CGFloat)buttonWidth {
  UIButton *newButton = [[[UIButton alloc] initWithFrame:CGRectMake(0.0, 0.0, buttonWidth, 28.0)] autorelease];
  newButton.titleLabel.font = aFont;
  newButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
  newButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  newButton.imageEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 5.0);
  [newButton setTitle:aTitle forState:UIControlStateNormal];
  [newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  UIImage *newImage = [[UIImage imageNamed:@"standardNavButton.png"] stretchableImageWithLeftCapWidth:8.0 topCapHeight:0.0];
  [newButton setBackgroundImage:newImage forState:UIControlStateNormal];
  UIImage *newPressedImage = [[UIImage imageNamed:@"standardNavButtonSelected.png"] stretchableImageWithLeftCapWidth:8.0 topCapHeight:0.0];
  [newButton setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];
  newButton.backgroundColor = [UIColor clearColor];
  if (anIcon) {
    [newButton setImage:anIcon forState:UIControlStateNormal];  
  }
  return newButton;
}


- (UIButton*)customButtonWithTitle:(NSString*)aTitle font:(UIFont*)aFont icon:(UIImage*)anIcon {
  CGFloat titleWidth = [aTitle sizeWithFont:aFont].width;
  CGFloat buttonWidth = anIcon ? anIcon.size.width + kSpaceBetweenIconAndText + titleWidth + kButtonFreeSpace : titleWidth + kButtonFreeSpace;
  return [self customButtonWithTitle:aTitle font:aFont icon:anIcon buttonWidth:buttonWidth];
}

- (void)buttonPressed {
  if ([super target] && [super action]) {
    [self.target performSelector:self.action withObject:self];
  }
}

@end
